<template>
   <div class="dialog" v-show="visible">
    <div class="dialog-wrap">
      <div class="dialog-header">
        <slot name="header" />
        <button class="dialog-header-close" @click="close">X</button>
      </div>
      <div class="dialog-body">
        <p><slot></slot></p>
      </div>
      <div class="dialog-footer">
        <slot name="footer"></slot>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Dialog",
  props:{
      visible:{
           type:Boolean,//数据类型
           require:true,//必须传
      }
  },
  methods:{
      close(){
           this.$emit('update:visible',false)
      }
  },
   watch:{
       //当对话框弹出来 的时候，后面的页面就不能动了，其实就是禁用滚动条
       visible:{
           immediate:true,//一开始就立即监视
           handler(){
                if(this.visible){
                    document.documentElement.style.overflow = 'hidden'
                }else{
                    document.documentElement.style.overflow = 'auto'

                }
           }
       }
   }
};
</script>

<style lang="less" scoped>
.dialog {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);

  .dialog-wrap {
    width: 460px;
    height: 150px;
    position: absolute;
    background-color: #fff;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 12px 20px;
    box-sizing: border-box; // 内减模式

    .dialog-header {
      display: flex;
      justify-content: space-between;
      span {
        font-size: 18px;
      }
      button {
        outline: none;
        border: none;
        cursor: pointer;
        font-size: 18px;
      }
    }

    .dialog-body {
      margin-top: 26px;
      text-align: center;
      font-size: 18px;
    }

    .dialog-footer {
      position: absolute;
      right: 30px;
      bottom: 12px;
      .btn {
        width: 46px;
        height: 32px;
        margin-left: 10px;
        cursor: pointer;
        font-size: 14px;
        outline: none;
        &.primary {
          background-color: rgb(102, 177, 255);
          color: #fff;
        }
      }
    }
  }
}
</style>